package com.starlight.flower_server.controller;

import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.loginInfo.cqrs.LoginInfoQueryAllCommand;
import com.starlight.flower_server.domain.loginInfo.data.LoginInfoData;
import com.starlight.flower_server.domain.service.LoginInfoService;
import com.starlight.flower_server.domain.service.captcha.CaptchaService;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.Status;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/19 8:53
 * 登录信息接口
 */
@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {

    @Resource
    private LoginInfoService loginInfoService;

    @Resource
    private CaptchaService captchaService;

    /**
     * 查询最近十条记录
     * @param id
     * @return
     * 登录记录列表
     */
    @GetMapping("/getLoginInfoListLatest10")
    public FlowerServerResponse<List<LoginInfoData>> getAllLoginInfoData(Long id){
        return FlowerServerResponse.success(loginInfoService.queryLoginInfoListByFlowerUserIdLatest10(id));
    }

    /**
     * 查询某个用户所有登录记录
     * 需要邮箱验证码验证
     * @param command
     * Long flowerUserId;
     * String email;
     * String captcha;
     * @return
     * 登录记录列表
     */
    @GetMapping("/getAllLoginInfoList")
    public FlowerServerResponse<List<LoginInfoData>> getAllLoginInfoList(LoginInfoQueryAllCommand command){
        if (captchaService.verifyCaptcha(command.getEmail(), command.getCaptcha())) {
            return FlowerServerResponse.success(loginInfoService.queryAllLoginInfoListByFlowerUserId(command.getFlowerUserId()));
        } else{
            throw new BadRequestException(Status.CAPTCHA_ERROR.getMessage(),Status.CAPTCHA_ERROR);
        }
    }

    @GetMapping("/getAllLoginInfoData")
    public FlowerServerResponse<TotalData<LoginInfoData>> getAllLoginInfoData(){
        return FlowerServerResponse.success(loginInfoService.queryAllLoginInfoData());
    }

    @GetMapping("/getLoginInfoTotalCount")
    public FlowerServerResponse<TotalData<LoginInfoData>> getLoginInfoTotalCount(){
        return FlowerServerResponse.success(loginInfoService.queryLoginInfoTotalCount());
    }


}
